package com.markus.code.排序.base;

/**
 * Author:markusZhang
 * Date:Create in 2020/8/4 17:47
 * todo:
 */
public class SelectSort {
    public static void selectSort(int []arr){
        if (arr == null || arr.length == 0){
            return ;
        }
        /**
         * 每次遍历i，都将i及后面最小的元素放到i上
         */
        for (int i=0;i<arr.length-1;i++){
            int minIndex= i;//最小元素的索引
            for (int j=i+1;j<arr.length;j++){
                minIndex = arr[j] < arr[minIndex] ? j : minIndex;
            }
            swap(arr,i,minIndex);
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static void main(String[] args) {
        int []arr = {3,4,2,5,7,6};
        selectSort(arr);
        for (int i : arr) {
            System.out.print(i+" ");
        }
    }
}
